const db = require('../../db')

/**
 * POST 部门的回调函数
 * @param unitName  部门名称
 * @param unitcode  部门代码
 * @param unitType  类型
 * @param remark  备注
 */
exports.add = (req, res) => {
  // 接收表单的数据
  const unitInfo = req.body
  let sql = 'select * from account_unit where unitName=?'
  db.query(sql, [unitInfo.unitName], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length > 0) {
      return res.cc('部门名称已被占用!')
    }
    // 定义 SQL 语句
    let sql1 = 'insert into account_unit set ?'
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, {
      unitName: unitInfo.unitName,
      unitType: unitInfo.unitType,
      unitAddress: unitInfo.unitAddress,
      remark: unitInfo.remark,
      createdTime: Date.now(), // 例如 createdTime 使用 Date.now() 来获取当前时间戳
    }, (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('新增部门失败！')
      return res.cc('新增部门成功', true)
    })
  })
}

exports.del = (req, res) => {
  // 接收表单的数据
  const unitInfo = req.body
  let sql = 'select * from account_unit where unitId=?'
  db.query(sql, [unitInfo.unitId], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    console.log('this1----', result)
    if (result.length === 0) {
      return res.cc('未找到该部门!')
    }
    // 定义 SQL 语句
    const sql1 = 'delete from account_unit where unitId=?'
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, unitInfo.unitId, (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('删除部门失败！')
      return res.cc('删除部门成功', true)
    })
  })
}

exports.update = (req, res) => {
  // 接收表单的数据
  const unitInfo = req.body
  let sql = 'select * from account_unit where unitId=?'
  db.query(sql, [unitInfo.unitId], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该部门!')
    }
    // 定义 SQL 语句
    const sql1 = "update account_unit set unitName=?, unitType=?, remark=?, unitAddress=? where unitId=?";
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, [unitInfo.unitName, unitInfo.unitType, unitInfo.remark, unitInfo.unitAddress, unitInfo.unitId], (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('修改部门失败！')
      return res.cc('修改部门成功', true)
    })
  })
}

exports.allList = (req, res) => {
  const page = req.body.page ? parseInt(req.body.page) : 1; // Get the page number, default to the first page
  const limit = req.body.limit ? parseInt(req.body.limit) : 10; // Get the number of items per page, default to 10
  const unitName = req.body.unitName || ''; // Get unitName from request body or set to an empty string
  const sqlCount = "SELECT COUNT(*) AS total FROM account_unit";
  db.query(sqlCount, (error, countResults) => {
    if (error) {
      return res.cc(error);
    }
    const total = countResults[0].total;
    const offset = (page - 1) * limit;
    let sql;
    let params;
    if (unitName) {
      sql = `SELECT * FROM account_unit WHERE unitName LIKE ? LIMIT ? OFFSET ?`;
      params = [`%${unitName}%`, limit, offset];
    } else {
      sql = `SELECT * FROM account_unit LIMIT ? OFFSET ?`;
      params = [limit, offset];
    }
    db.query(sql, params, (error, results) => {
      if (error) {
        return res.cc(error);
      }
      return res.cc('获取部门成功', true, results, {
        total,
        page,
        limit
      });
    });
  });

}
